<?php
namespace app\index\controller;
use stock\other\PageSql;
use \think\Request;
use think\Db; 
use think\Cache;
class Index extends \app\index\controller\Common
{
	public $dbinfo='';
    protected $beforeActionList = [
	    'lay_do',
        'f_init',
	];
    //开始下载
    
    public function toDownload ($fileName,$strTable)
    {   
        header("Content-type: application/vnd.ms-excel");
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=".$fileName."_".date('Y-m-d').".xls");
        header('Expires:0');
        header('Pragma:public');
        $header='<html xmlns:o="urn:schemas-microsoft-com:office:office" 
                     xmlns:x="urn:schemas-microsoft-com:office:excel" 
                     xmlns="http://www.w3.org/TR/REC-html40"> 
                     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
                     <html> 
                     <head> 
                        <meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
                         <style id="Classeur1_16681_Styles"></style> 
                     </head> 
                         <body> 
                        <div id="Classeur1_16681" align=center x:publishsource="Excel"> ';
        $foot='</div> 
                 </body> 
             </html>';              
        echo $header.$strTable.$foot;
    }
    public function details(){
        $request = request()->param();
        if($request['id']){ 
            $key_name='details_'.$request['id'];
            if(VISIT_SERVICE==1){
                $t_key=$key_name.'_pc';
                $list=Cache::get($t_key);
                if(!$list){                  
                    $goodsObj=Db::name('image_content');
                    $list=$goodsObj->where('image_id='.$request['id'])->order('id ASC')->select();
                    Cache::set($t_key,$list,60*60*24*60);
                }
                $this->assign('list', $list); 
                return $this->fetch('details2');
            }else{
                $t_key=$key_name.'_mobile';
                $d_all=Cache::get($t_key); 
                if(!$d_all){     
                    $info=Db::table('img_image_main')->alias('a')->where('a.id',$request['id'])->join('img_source_user b','a.source=b.id','LEFT')->order("a.id desc")->field('a.*,b.name as bname')->find();
                    $goodsObj=Db::name('image_content');
                    $list=$goodsObj->where('image_id='.$request['id'])->order('id DESC')->select(); 
                    Cache::set($t_key,array('list'=>$list,'info'=>$info),60*60*24*60);                  
                }else{
                    $list=$d_all['list'];
                    $info=$d_all['info'];
                }  
                $this->assign('list', $list); 
                $this->assign('title', $info['title']); 
                $this->assign('keywords', $info['keywords']); 
                $this->assign('content', $info['content']);
                $this->assign('source', $info['bname']);
                return $this->fetch();
            }               
        }
    }
    public function details3(){
        return $this->fetch();
    }
    public function details4(){
        return $this->fetch();
    }
    public function index4(){
        return $this->fetch();
    }

    public function index6(){
        return $this->fetch('index6');
    }
    public function list(){
        return $this->fetch('list');
    }  
    public function index()
    {   
        $request = request()->param();
        $t='';
        $ty='';
        if($request['t']&&is_numeric($request['t'])){
            $where =' and theme='.(int)$request['t'];
            $ty=$request['t'];
            $t=$request['t']; 
            $this->assign('t', $t);
        }else{
            $where =' and theme!=3';
        }
        $this->assign('ty', $ty);
        $key_name='index_'.$t;
        if(VISIT_SERVICE==1){ 
            $t_key=$key_name.'_mo';
            $list=Cache::get($t_key);
            if(!$list){
                $contentObj=Db::name('image_content');
                $list=Db::table('img_image_main')->alias('a')->where('a.status=0'.$where)->join('img_source_user b','a.source=b.id','LEFT')->order("a.id desc")->field('a.*,b.name as bname')->limit(6)->select();
                foreach($list as $k=>$v){
                    if($v['id']){
                        $id=$v['id'];
                        $list[$k]['image_url']=$contentObj->where('image_id='.$v['id'])->limit(9)->select();
                    }
                }
                Cache::set($t_key,$list,60*60*24); 
            }
            $this->assign('list', $list);
            return $this->fetch('index2');
        }else{
            $moban=$ty ? 'list' : 'index6';
            $n_page=$request['page'] ?? 1;
            $t_key=$key_name.'_pc_';
            $list_key="list_".$ty."_".$n_page;
            $new_key="new_".$ty;
            $hot_key="hot_".$ty;

            $star_key="star_";
            $live_key="live_";
            $huanden_key="huanden_key";
            $jiepai_key="jiepai_";
            $list=Cache::get($list_key);
            $star= Cache::get($star_key);
            $new= Cache::get($new_key); 
            $hot= Cache::get($hot_key); 
            $live= Cache::get($live_key); 
            $huanden= Cache::get($huanden_key); 
            $jiepai= Cache::get($jiepai_key);               
            if((!$list&&$ty)||(!$ty&&!$huanden)){
                $goodsObj=Db::name('image_main');
                $p_id=mt_rand(1,100);
                if($ty){
                    $list=$goodsObj->where('status=0'.$where)->order('id DESC')->paginate(20);;
                    $new=Db::name('image_main')->where('status=0'.$where)->limit($p_id,15)->order('id DESC')->select();
                    $p_id1=mt_rand(1,100);
                    $hot=Db::name('image_main')->where('status=0'.$where)->limit($p_id1,10)->order('id DESC')->select();
                    Cache::set($list_key,$list,60*60*24);
                    Cache::set($new_key,$new,60*60*24); 
                    Cache::set($hot_key,$hot,60*60*24);                     
                }else{
                    $huanden=$goodsObj->where('status=0'.$where)->limit($p_id,5)->order('id DESC')->select();
                    $new=Db::name('image_main')->where('status=0'.$where)->limit(29)->order('id DESC')->select();
                    $hot=Db::name('image_main')->where('status=0'.$where)->limit(15)->order('id DESC')->select();
                    $jiepai=Db::name('image_main')->where('status=0 and theme=1')->limit(15)->order('id DESC')->select();
                    $star=Db::name('image_main')->where('status=0 and theme=4')->limit(15)->order('id DESC')->select();
                    $live=Db::name('image_main')->where('status=0 and theme=2')->limit(15)->order('id DESC')->select();
                    Cache::set($star_key,$star,60*60*24);
                    Cache::set($new_key,$new,60*60*24); 
                    Cache::set($hot_key,$hot,60*60*24); 
                    Cache::set($live_key,$live,60*60*24); 
                    Cache::set($huanden_key,$huanden,60*60*24); 
                    Cache::set($jiepai_key,$jiepai,60*60*24);             
                }
                //$list=Db::table('img_image_main')->alias('a')->where('a.status=0'.$where)->join('img_source_user b','a.source=b.id','LEFT')->order("a.id desc")->field('a.*,b.name as bname')->paginate(10);
                //Cache::set($t_key,$list,60*60*24); 
            }
            //echo $page.'_'.$goodsObj->getLastSql();exit;
            //print_r($list);exit;
            $this->assign('list', $list);
            $this->assign('star', $star); 
            $this->assign('jiepai', $jiepai); 
            $this->assign('live', $live); 
            $this->assign('hot', $hot); 
            $this->assign('new', $new);  
            $this->assign('huanden', $huanden);  
            //$this->assign('list', $list);
            return $this->fetch($moban);
        }       
        //'index_mob'        
    }
    public function imagelist(){
        $goodsObj=Db::name('image_main');
        if(VISIT_SERVICE==1){
            $per_page=6;
        }else{
            $per_page=10;
        }   
        
        $request = request()->param();//$request['page']=2;
        if($request['t']&&is_numeric($request['t'])){
            $where =' and a.theme='.(int)$request['t'];
        }        
        $page=$request['page'];
        $list=Db::table('img_image_main')->alias('a')->where('a.status=0'.$where)->join('img_source_user b','a.source=b.id','LEFT')->order("a.id desc")->field('a.*,b.name as bname')->limit((($page-1)*$per_page),$per_page)->select();
        if(VISIT_SERVICE==1){
            $contentObj=Db::name('image_content');
            foreach($list as $k=>$v){
                if($v['id']){
                    $id=$v['id'];
                    $list[$k]['image_url']=$contentObj->where('image_id='.$v['id'])->limit(9)->select();
                }
            }
        }
        //echo $page.'_'.$goodsObj->getLastSql();
        //exit(Db::name('image_main')->getLastSql());
        $result['status']=count($list) ? '1' : '0';
        $result['data']=$list;
        echo json_encode($result);       
    }
    public function g_mkdirs($dir, $mode = 0777){
        if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
        if (!$this->g_mkdirs(dirname($dir), $mode)) return FALSE;
     
        return @mkdir($dir, $mode);
    }

    public function image_thumb_content(){
        $goodsObj=Db::name('image_content');
        $result=$goodsObj->where("thumb='0' and image_id!=4")->limit(600)->select();//echo $goodsObj->getLastSql();EXIT;
        $dir='./thumb_content/'.date('Ymd').'/';
        $dir_tmp='/thumb_content/'.date('Ymd').'/';
        if(!is_dir($dir)){
            $this->g_mkdirs($dir);
        }
        foreach ($result as $key => $value) {
            if($value['url']){
                //$value['url']='/uploads/2017-09-28/6B08B5F878061D4E1345BE86C79379CC%20%281%29.jpg';
                
                $new_filename=explode('/', $value['url']);//print_r($new_filename);exit;
                
                if(empty($new_filename[3])){
                    unset($new_filename[3]);
                    $img_name=$new_filename[4];
                }else{
                    $img_name=$new_filename[3];
                }
                    
                $value['url']=implode('/', $new_filename);//echo $img_name.'<br>'.$value['url'];//exit;
                
                //$img_name=$new_filename[3];
                if($img_name){//echo $value['url'];exit;
                    $thumb_name=$dir."thumb_source_".date('s').$img_name;//exit($thumb_name);
                    $thumb_url=$dir_tmp."thumb_source_".date('s').$img_name;//exit($value['url']);
                    //echo '.'.$value['url'];               
                    $image = \think\Image::open('.'.$value['url']);
                    // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                    $image->thumb(200, 200)->save($thumb_name);
                    $data=array();
                    $data['thumb']=$thumb_url;
                    $goodsObj->where('id='.$value['id'])->update($data);
                }
            }
        }
    }    
    public function image_thumb(){
        $goodsObj=Db::name('image_main');
        $result=$goodsObj->where('thumb=0 and theme!=3')->limit(600)->order('id desc')->select();
        $dir='./thumb/'.date('Ymd').'/';
        $dir_tmp='/thumb/'.date('Ymd').'/';
        if(!is_dir($dir)){
            $this->g_mkdirs($dir);
        }
        foreach ($result as $key => $value) {
            if($value['covers']){
                $new_filename=explode('/', $value['covers']);//print_r($new_filename);exit;
                if($new_filename[3]){
                    $thumb_name=$dir."thumb_".$new_filename[3];//exit($thumb_name);
                    $thumb_url=$dir_tmp."thumb_".$new_filename[3];//exit('.'.$value['covers']);
                    $image = \think\Image::open('.'.$value['covers']);//exit;
                    // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                    $image->thumb(400, 500)->save($thumb_name);
                    $data=array();
                    $data['thumb']=$thumb_url;
                    $goodsObj->where('id='.$value['id'])->update($data);
                }
            }
        }
        /*           
        $image = \think\Image::open('./covers/20170928/8b3ce1f7d5456d666576edeb9fb40ddd.jpg');
        $dir='./thumb/20170928/';
        if(!is_dir($dir)){
            $this->g_mkdirs($dir);
        }
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
        $image->thumb(200, 200)->save('./thumb/thumb.png'); 
        */    
    }

    public function qk(){
        Cache::clear(); 
    }

    public function f_init(){
        $this->view->engine->layout(false);
    }
    public function hh(){
        phpinfo();
    }
    public function lay_do(){
        error_reporting(E_ALL & ~E_NOTICE);
    }
 
}
